﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ELGGAPI.DomainModels;
using ELGGAPI.Models;
using ELGGAPI.Data;
using System.Web.Http;


namespace ELGGAPI.Controllers
{
    public class PlayerController : ApiController
    {
        //TODO Get Player List
        public Response<IEnumerable<PlayerModel>> GetPlayerlist(string access_token)
        {
            var response = new Response<IEnumerable<PlayerModel>>();
            try
            {
                var user = AutheticationToken.ValidateToken(access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }

                response.Data = PlayerDomainModles.GetPlayerList();
                response.Message = null;

                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }
        public Response<Idclass> PostAddMetrics(Request<PlayerMetrics> request)
        {
            var response = new Response<Idclass>();
            try
            {
                var user = AutheticationToken.ValidateToken(request.access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                 PlayerMetrics playerMetrics = PlayerDomainModles.AddGameMatrics(request.Data);

                 if (!playerMetrics.gameid.HasValue)
                 {
                     response.Data = null;
                     response.Message = "Invalid GameId.";
                     return response;
                 }
                 if (!playerMetrics.playerid.HasValue)
                 {
                     response.Data = null;
                     response.Message = "Invalid PlayerId.";
                     return response;
                 }
                 if (playerMetrics.playerid== -100)
                {
                    response.Data = null;
                    response.Message = "Unable to add duplicate metrics.";
                    return response;
                }

                 response.Data.id = int.Parse(playerMetrics.playerid.ToString());
                 response.Message = null;
                 return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }
        public Response<Idclass> PostRegisterAsPlayer(Request<Idclass> request)
        {
            Response<Idclass> response = new Response<Idclass>();
            try
            {
                if (AutheticationToken.ValidateToken(request.access_token) == null)
                {
                    response.Data = (Idclass)null;
                    response.Message = "Token is invalid";
                    return response;
                }
                else
                {
                    Idclass idclass = PlayerDomainModles.RegisterUserAsPlayer(request.Data);
                    if (idclass.id == -100)
                    {
                        response.Data = (Idclass)null;
                        response.Message = "Player id is invalid";
                        return response;
                    }
                    else
                    {
                        response.Data = idclass;
                        response.Message = null;
                        return response;
                    }
                }
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                response.Data = null;
                return response;
            }
        }

        public Response<IEnumerable<ProfileImage>> GetProfileImagesBinary(string playerid, string access_token)
        {
            var response = new Response<IEnumerable<ProfileImage>>();
            try
            {
                var user = AutheticationToken.ValidateToken(access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }

                int intPlayerId;
                bool res = int.TryParse(playerid, out intPlayerId);
                if (res == false)
                {
                    response.Data = null;
                    response.Message = "PlayerId is invalid";
                    return response;
                }
                response.Data = PlayerDomainModles.GetPlayerProfileImagesBinary(intPlayerId);
                response.Message = null;
                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }
        public Response<IEnumerable<ProfileImage>> GetProfileImages(string playerid, string access_token)
        {

            var response = new Response<IEnumerable<ProfileImage>>();
            try
            {
                var user = AutheticationToken.ValidateToken(access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }

                int intPlayerId;
                bool res = int.TryParse(playerid, out intPlayerId);
                if (res == false)
                {
                    response.Data = null;
                    response.Message = "PlayerId is invalid";
                    return response;
                }
                response.Data = PlayerDomainModles.GetPlayerProfileImages(intPlayerId);
                response.Message = null;
                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }
        public Response<PlayerMetrics> GetMetrics(string playerid, string gameid, string access_token)
        {
            var response = new Response<PlayerMetrics>();
            try
            {
                var user = AutheticationToken.ValidateToken(access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                int PlayerId = 0;
                int.TryParse(playerid, out PlayerId);

                int GameId = 0;
                int.TryParse(gameid, out GameId);

                PlayerMetrics playermetrics = PlayerDomainModles.GetGameMetrics(PlayerId, GameId);
                response.Data = playermetrics;
                response.Message = null;


                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }
        public Response<IEnumerable<PlayerModel>> GetPortalUsers(string access_token)
        {
            var response = new Response<IEnumerable<PlayerModel>>();
            
            try
            {
                if (AutheticationToken.ValidateToken(access_token) == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                else
                {
                    response.Data = PlayerDomainModles.GetPortalUsers();
                    response.Message = null;
                    return response;
                }
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                response.Data = null;
                return response;
            }
            
        }

        public Response<PlayerModel> GetPlayerInfoByToken(string access_token)
        {
            var response = new Response<PlayerModel>();
            try
            {
                var user = AutheticationToken.ValidateToken(access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                
                PlayerModel Player = PlayerDomainModles.GetPlayerInfoByToken(access_token);
                response.Data = Player;
                response.Message = null;


                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }

        }

        public Response<PlayerModel> GetPlayerInfo(string playerid, string access_token)
        {
            var response = new Response<PlayerModel>();
            try
            {
                var user = AutheticationToken.ValidateToken(access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                int PlayerId = 0;
                int.TryParse(playerid, out PlayerId);


                PlayerModel Player = PlayerDomainModles.GetPlayerInfo(PlayerId);
                response.Data = Player;
                response.Message = null;


                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }

        }
        public Response<IEnumerable<PlayeronlineModel>> GetOnlineStatus(string playerid, string status, string showfriends, string access_token)
        {
            var response = new Response<IEnumerable<PlayeronlineModel>>();
            try
            {
                var user = AutheticationToken.ValidateToken(access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                int PlayerId = 0;
                int.TryParse(playerid, out PlayerId);

                int Status = -100;
                if (string.IsNullOrEmpty(status))
                    Status = -100;
                else
                    int.TryParse(status, out Status);
                int ShowFriends = 0;
                if (string.IsNullOrEmpty(showfriends))
                    ShowFriends = 0;
                else
                    int.TryParse(showfriends, out ShowFriends);

                IEnumerable<PlayeronlineModel> Players = PlayerDomainModles.GetPlayerstatus(PlayerId, Status, ShowFriends);
                response.Data = Players;
                response.Message = null;

                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }

        }

        public Response<AutheticationToken> GetAuthenticate(string username, string password)
        {
            var response = new Response<AutheticationToken>();
            try
            {

                AutheticationToken AuthToken = AutheticationToken.Authenticate(username, password, 2);
                if (AuthToken == null)
                {
                    response.Data = null;
                    response.Message = "Authentication failed";
                }
                response.Data = AuthToken;
                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }



        public Response<Idclass> PostAddGamePlayer(Request<PlayerModel> request)
        {
            var response = new Response<Idclass>();
            try
            {
                var user = AutheticationToken.ValidateToken(request.access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                Idclass Id = PlayerDomainModles.AddPlayer(request.Data);

                if (Id.id == -100)
                {
                    response.Data = null;
                    response.Message = "User name duplicated.";
                    return response;
                }

                response.Data = Id;

                response.Message = null;
                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }
        public Response<Idclass> PostInterestedgame(Request<InterestedGame> request)
        {
            var response = new Response<Idclass>();
            try
            {
                var user = AutheticationToken.ValidateToken(request.access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                Idclass Id = PlayerDomainModles.AddInterestedGame(request.Data);

                if (Id.id == -100)
                {
                    response.Data = null;
                    response.Message = "Unable to add interested game.";
                    return response;
                }

                response.Data = Id;

                response.Message = null;
                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }
        public Response<IEnumerable<GroupModel>> GetAllGroup(string access_token)
        {
            Response<IEnumerable<GroupModel>> response = new Response<IEnumerable<GroupModel>>();
            try
            {
                if (AutheticationToken.ValidateToken(access_token) == null)
                {
                    response.Data = (IEnumerable<GroupModel>)null;
                    response.Message = "Token is invalid";
                    return response;
                }
                else
                {
                    response.Data = (IEnumerable<GroupModel>)PlayerDomainModles.GetGroups(1);
                    response.Message = (string)null;
                    return response;
                }
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                response.Data = (IEnumerable<GroupModel>)null;
                return response;
            }
        }
        public Response<Idclass> PostAddContact(Request<PlayerContact> request)
        {
            var response = new Response<Idclass>();
            try
            {
                var user = AutheticationToken.ValidateToken(request.access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                if (request.Data.contactid == null)
                    request.Data.contactid = 0;
                if (request.Data.elggentityid == null)
                    request.Data.elggentityid = 0;

                Idclass Id = PlayerDomainModles.AddContact(request.Data);

                response.Data = Id;
                response.Message = null;
                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }


    }
}
